﻿#ifndef WIDGETPLCCLIENT_H
#define WIDGETPLCCLIENT_H
#include <QWidget>
#include <QGroupBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QComboBox>
#include <QHBoxLayout>
#include <QListWidget>
#include <QTableWidget>
#include "PLCClientBase.h"
#include "WidgetReadCfg.h"

class WidgetPLCClient : public QGroupBox
{
    Q_OBJECT
public:
    explicit WidgetPLCClient(const QString& title = "",QWidget* parent = nullptr);

    void addPLCClient(PLCClientBase* client);

signals:
    void sigConfigChanged(const PLCClientConfig& config);
    void sigAddOne(const PLCClientConfig& config);
    void sigDeleteOne(const QString& name);
private:
    void setupUI();
    void setupPLCConfig();
    void setupComConfig();
    void setupSendRev();
    void initConnection();

    PLCClientConfig getConfig();
    void onSaveConfig();
    void onComSelectChanged(const int& index);
    QString getTxt(int index);
    double getValue(int value);

private:
    QMap<QString, PLCClientBase*> mLstPLCClient;

    //control
    QHBoxLayout* mLayout;
    //PLC配置控件
    QGroupBox* grpboxPLConfig;
    QPushButton* pbtnAddOne;
    QPushButton* pbtnDeleteOne;
    QPushButton* pbtnApplyPLC;
    QComboBox* cbboxComNameList;
    QComboBox* cbboxPLCClientTypeList;
    QSpinBox* spboxServerID;
    QSpinBox* spboxWriteSpace;
    QComboBox* cbboxIsReadConstant;
    QWidget* wgtReadConstantConfig;
    QSpinBox* spboxReadSpace;
    WidgetReadCfg* wgtReadCfg;

    //端口配置
    QGroupBox* grpboxComConfig;
    //串口
    QWidget* wgtSerialConfig;
    QComboBox* cbboxSerialList;
    QComboBox* cbboxSerialBaudRate;
    QComboBox* cbboxSerialDataBits;
    QComboBox* cbboxSerialParity;
    QComboBox* cbboxSerialStopBits;
    QComboBox* cbboxSerialFlowControl;
    //socket
    QWidget* wgtSocketConfig;
    QLineEdit* ledtOpponentIP;
    QSpinBox* spboxOpponentPort;
    QLineEdit* ledtLocalIP;
    QSpinBox* spboxLocalPort;
    //udp
    QWidget* wgtSendType;
    QComboBox* cbboxUdpSendType;

    QCheckBox* chkboxSendHex;

    //接收区
    QCheckBox* chkboxIsDisplayData;
    QPushButton* pbtnClear;
    QTextEdit* txtedtDataRev;

    //发送区
    QSpinBox* spboxAddrStart;
    QComboBox* cbboxDataType;
    QTableWidget* tblwgtReadList;
    QPushButton* pbtnDataAdd;
    QPushButton* pbtnDataRemove;
    QPushButton* pbtnDataRead;
    QPushButton* pbtnDataWrite;
    bool mIsRequest = false;


};

#endif
